﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebEntities;
using WebEntities.Models.WebInfo;

namespace com.subb.WebInformation
{
    public class WI_FriendlyLinkDAL
    {
        public List<WI_FriendlyLink> GetAllFriendlyLink(int start, int limit, out int ttlCount)
        {
            ttlCount = 0;
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.WI_FriendlyLink.OrderBy(c => c.SortNum);
                ttlCount = query.Count();
                List<WI_FriendlyLink> catalist = query.Skip(start).Take(limit).ToList();
                return catalist;
            }
        }

        public List<WI_FriendlyLink> GetAllFriendlyLink()
        {
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.WI_FriendlyLink.OrderBy(c => c.SortNum);
                List<WI_FriendlyLink> catalist = query.ToList();
                return catalist;
            }
        }

        public WI_FriendlyLink GetFriendlyLink(int id)
        {
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                WI_FriendlyLink ncata = ctx.WI_FriendlyLink.SingleOrDefault(c => c.FLID == id);
                return ncata;
            }
        }


        public void CreateFriendlyLink(WI_FriendlyLink addObj)
        {
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                ctx.WI_FriendlyLink.AddObject(addObj);
                ctx.SaveChanges();
            }
        }

        public bool UpdateFriendlyLink(int id, FriendlyLink uptObj)
        {
            bool result = false;
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                WI_FriendlyLink obj = ctx.WI_FriendlyLink.SingleOrDefault(c => c.FLID == id);
                if (obj != null)
                {
                    obj.FriendlyName = uptObj.FriendlyName;
                    obj.SortNum = uptObj.SortNum;
                    obj.WebLink = uptObj.WebLink;
                    obj.IsShow = byte.Parse(uptObj.IsShow.ToString());
                    if (!string.IsNullOrEmpty(uptObj.FriendlyLogo))
                    {
                        obj.FriendlyLogo = uptObj.FriendlyLogo;
                    }

                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }

        public bool RemoveFriendlyLink(int id)
        {
            bool result = false;
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                WI_FriendlyLink obj = ctx.WI_FriendlyLink.SingleOrDefault(c => c.FLID == id);
                if (obj != null)
                {
                    ctx.WI_FriendlyLink.DeleteObject(obj);
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }
    }
}
